package com.kuang.controller;



import com.kuang.pojo.Books;
import com.kuang.pojo.Readers;
import com.kuang.service.ReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/reader")
public class ReaderController {

    @Autowired
    @Qualifier("ReaderServiceImpl")
    private ReaderService readerService;

    //查询全部读者
    @RequestMapping("/allReader")
    public String list(Model model){
        List<Readers> list = readerService.queryAllReader();
        model.addAttribute("list",list);
        return "readers/allReader";
    }

    //增加读者
    @RequestMapping("/toAddReader")
    public String toAddPaper(){
        return "readers/addReader";
    }

    //增加读者的请求
    @RequestMapping("/addReader")
    public String addReader(Readers readers){
        readerService.addReader(readers);
        return "redirect:/reader/allReader";
    }

    //修改读者信息
    @RequestMapping("/toUpdate")
    public String toUpdatePaper(int id,Model model){System.out.println(id);
        Readers readers=readerService.queryReaderById(id);
        model.addAttribute("Qreader",readers);
        return "readers/updateReader";
    }

    //修改读者信息的请求
    @RequestMapping("/updateReader")
    public String updateReader(Readers readers){
        readerService.updateReader(readers);
        return "redirect:/reader/allReader";
    }

    //删除读者信息
    @RequestMapping("/deleteReader/{readerId}")
    public String deleteReader(@PathVariable("readerId") int id){
        readerService.deleteReaderById(id);
        return "redirect:/reader/allReader";
    }

    //使用读者编号查询读者信息
    @RequestMapping("/querReader")
    public String querReader(String queryReadersNo,Model model){
        List<Readers> readers = readerService.queryReaderByNo(queryReadersNo);
        List<Readers> list = new ArrayList<Readers>();
        list.addAll(readers);
        if (readers==null){
            list = readerService.queryAllReader();
            model.addAttribute("error",list);
        }
        model.addAttribute("list",list);
        return "readers/allReader";
    }

    @RequestMapping("/back")
    public String back(){
        return "index/index";
    }
}
